home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / demo / GDS_COMPLEX.lha / complex / left_right.c < prev    next >
C/C++ Source or Header  |  1994-04-15  |  8KB  |  360 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5.  
  6. #include <exec/memory.h>
  7. #include <exec/types.h>
  8. #include <graphics/gfxbase.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/graphics_protos.h>
  12.  
  13. #include "GameSmith:include/libraries/libraries.h"
  14. #include "GameSmith:include/libraries/libptrs.h"
  15. #include "GameSmith:GameSmith.h"
  16.  
  17. /*-------------------------------------------------------------------------*/
  18. /* Function Prototypes                                                                       */
  19.  
  20. void parser(int,char **);
  21. int setup(void);
  22. void move_image(void);
  23. void check_bounds(void);
  24. int check_close(void);
  25. void cleanup(void);
  26. void free_arrays(void);
  27.  
  28. /*-------------------------------------------------------------------------*/
  29.  
  30. int cplx_cnt,swidth=320,sheight=200,smode=0,delay=0;
  31. int *x=NULL,*y=NULL,*speedx=NULL,*speedy=NULL,*bounce=NULL;
  32. char *file;
  33. int dlist;                    /* display list for anims */
  34.  
  35. struct anim_cplx *cplx;
  36.  
  37. struct display_struct *display;
  38.  
  39. /*-------------------------------------------------------------------------*/
  40.  
  41. main(argc,argv)
  42. int argc;
  43. char *argv[];
  44.  
  45. {
  46.     int err,end=0;
  47.  
  48.     if (argc < 3)
  49.         {
  50.         printf("\nUSAGE: left_right {filename} [number of objects] ([HIRES] | [LACE]) [VB delay]\n");
  51.         exit(01);
  52.         }
  53.     if (gs_open_libs(DOS|GRAPHICS,0))
  54.         exit(02);                    /* if can't open libs, abort */
  55.     parser(argc,argv);            /* parse command line args */
  56.     if (err=setup())                /* if couldn't get set up... abort program */
  57.         {
  58.         printf("\nsetup error %d\n",err);
  59.         gs_close_libs();            /* close all libraries */
  60.         exit(03);
  61.         }
  62.     Forbid();
  63.     while (!end)
  64.         {
  65.         move_image();
  66.         end=check_close();        /* end when user hits left mouse */
  67.         }
  68.     Permit();
  69.     cleanup();                        /* close & deallocate everything */
  70.     gs_close_libs();                /* close all libraries */
  71. }
  72.  
  73. /***************************************************************************/
  74.  
  75. void parser(argc,argv)
  76. int argc;
  77. char *argv[];
  78.  
  79. {
  80.     file=argv[1];                    /* remember file name */
  81.     cplx_cnt=atoi(argv[2]);        /* number of complexes running around screen */
  82.     if (argc >= 4)
  83.         {
  84.         if (!(stricmp(argv[3],"HIRES")))    /* check for hires spec */
  85.             {
  86.             swidth=640;
  87.             sheight=400;
  88.             if (GfxBase->LibNode.lib_Version >= 36)
  89.                 {
  90.                 if (ModeNotAvailable(DBLNTSCHIRESFF_KEY))
  91.                     smode=HIRES|LACE;
  92.                 else
  93.                     smode=DBLNTSCHIRESFF_KEY;
  94.                 }
  95.             else
  96.                 smode=HIRES|LACE;
  97.             }
  98.         else if (!(stricmp(argv[3],"SUPER")))    /* check for superhires */
  99.             {
  100.             if (GfxBase->LibNode.lib_Version >= 36)
  101.                 {
  102.                 if (ModeNotAvailable(SUPER72_MONITOR_ID | SUPERLACE_KEY))
  103.                     {
  104.                     swidth=640;
  105.                     sheight=400;
  106.                     smode=HIRES|LACE;
  107.                     }
  108.                 else
  109.                     {
  110.                     smode=SUPER72_MONITOR_ID | SUPERLACE_KEY;
  111.                     swidth=800;
  112.                     sheight=600;
  113.                     }
  114.                 }
  115.             else
  116.                 {
  117.                 swidth=640;
  118.                 sheight=400;
  119.                 smode=HIRES|LACE;
  120.                 }
  121.             }
  122.         else if (!(stricmp(argv[3],"HAM")))    /* check for HAM mode */
  123.             {
  124.             smode|=HAM_KEY;
  125.             }
  126.         }
  127.     if (argc >= 5)
  128.         {
  129.         if (!(stricmp(argv[4],"HAM")))        /* check for HAM mode */
  130.             {
  131.             smode|=HAM_KEY;
  132.             }
  133.         else
  134.             {
  135.             delay=atoi(argv[4]);    /* number vertical blank delay intervals between anim updates */
  136.             }
  137.         }
  138. }
  139.  
  140. /***************************************************************************/
  141.  
  142. setup()
  143.  
  144. {
  145.     int cnt,seq,depth=0;
  146.     struct anim_struct *anim;
  147.     struct blit_struct *img;
  148.  
  149.     struct anim_load_struct load =
  150.         {
  151.         NULL,                                    /* name of anim file to load */
  152.         NULL,                                    /* ptr to anim upon return */
  153.         NULL,                                    /* ptr to attachment array specification upon return */
  154.         NULL,                                    /* ptr to color map upon return */
  155.         0,                                        /* # entries in the color map */
  156.         8,                                        /* # bits per color map entry */
  157.         0,                                        /* type (filled). 0 = anim, 1 = complex */
  158.         1,                                        /* # objects to allocate in array */
  159.         0                                        /* flags */
  160.         };
  161.  
  162.     if (!(x=(int *)malloc(cplx_cnt*sizeof(int))))
  163.         return(-1);
  164.     if (!(y=(int *)malloc(cplx_cnt*sizeof(int))))
  165.         {
  166.         free_arrays();
  167.         return(-1);
  168.         }
  169.     if (!(speedx=(int *)malloc(cplx_cnt*sizeof(int))))
  170.         {
  171.         free_arrays();
  172.         return(-1);
  173.         }
  174.     if (!(speedy=(int *)malloc(cplx_cnt*sizeof(int))))
  175.         {
  176.         free_arrays();
  177.         return(-1);
  178.         }
  179.     if (!(bounce=(int *)malloc(cplx_cnt*sizeof(int))))
  180.         {
  181.         free_arrays();
  182.         return(-1);
  183.         }
  184.     for (cnt=0; cnt < cplx_cnt; cnt++)
  185.         bounce[cnt]=0;
  186.     load.filename=file;                    /* user specified anim file name */
  187.     load.array_elements=cplx_cnt;        /* user specified # elves */
  188.     if (gs_load_anim(&load))
  189.         {
  190.         free_arrays();
  191.         return(-1);
  192.         }
  193.     cplx=load.anim_ptr.cplx;            /* ptr to anim complex */
  194.     if (load.type != 1)                    /* make sure it's an anim complex */
  195.         {
  196.         free_arrays();
  197.         if (load.type == 0)
  198.             gs_free_anim((struct anim_struct *)cplx,cplx_cnt);
  199.         return(-2);
  200.         }
  201.     anim = cplx[0].list;                    /* ptr to 1st anim in complex */
  202.     while (anim)
  203.         {
  204.         img = anim->list;                    /* ptr to 1st image in anim sequence */
  205.         while (img)                            /* find max depth of anim */
  206.             {
  207.             if (img->depth > depth)
  208.                 depth = img->depth;
  209.             if (img->next == img)        /* avoid infinite loop */
  210.                 img=NULL;
  211.             else
  212.                 img=img->next;
  213.             }
  214.         anim=anim->cplx_next;            /* next anim in complex */
  215.         }
  216.     if (!(display=gs_display(swidth,sheight,depth,smode,GSV_DOUBLE,load.cmap)))
  217.         {
  218.         free_arrays();
  219.         FreeMem(load.cmap,load.cmap_entries*sizeof(long));
  220.         gs_free_cplx(cplx,cplx_cnt);
  221.         return(-3);
  222.         }
  223.     FreeMem(load.cmap,load.cmap_entries*sizeof(long));
  224.     if ((dlist=gs_get_display_list()) < 0)
  225.         {
  226.         cleanup();
  227.         return(-4);
  228.         }
  229.     gs_init_anim(dlist,display->vp->bitmap1,display->vp->bitmap2);
  230.     gs_set_anim_bounds(dlist,0,0,swidth-1,sheight-1);
  231.     for (cnt=0; cnt < cplx_cnt; cnt++)
  232.         {
  233.         x[cnt] = gs_random(swidth);            /* random X,Y coords */
  234.         y[cnt] = gs_random(sheight);
  235.         while ((speedx[cnt] = gs_random(11)) < 3);
  236.         while ((speedy[cnt] = gs_random(4)) == 0);
  237.         if (cnt&1)
  238.             {
  239.             speedx[cnt]*=-1;
  240.             speedy[cnt]*=-1;
  241.             seq=0;                            /* left moving anim */
  242.             }
  243.         else
  244.             seq=1;                            /* right moving anim */
  245.         if (gs_add_anim_cplx(dlist,&cplx[cnt],x[cnt],y[cnt],seq,cplx[cnt].list->prio))
  246.             {
  247.             cleanup();                        /* release everything */
  248.             return(-5);                        /* return failure */
  249.             }
  250.         }
  251.     gs_draw_anims(dlist);
  252.     check_bounds();
  253.     gs_next_anim_page(dlist);
  254.     gs_show_display(display,1);
  255.     gs_flip_display(display,1);
  256.     gs_open_vb_timer();
  257.     return(0);
  258. }
  259.  
  260. /***************************************************************************/
  261.  
  262. void move_image()
  263.  
  264. /* move and animate the graphic objects on the screen */
  265.  
  266. {
  267.     int cnt;
  268.  
  269.     if (gs_vb_time() < delay)
  270.         return;
  271.     gs_vb_timer_reset();
  272.     for (cnt=0; cnt < cplx_cnt; cnt++)
  273.         {
  274.         x[cnt]+=speedx[cnt];
  275.         y[cnt]+=speedy[cnt];
  276.         gs_anim_cplx(&cplx[cnt],x[cnt],y[cnt]);
  277.         }
  278.     while (display->flags & GSV_FLIP);    /* while display not flipped */
  279.     gs_draw_anims(dlist);                    /* draw them anim objects! */
  280.     check_bounds();                        /* keep a watch on them pesky keeblers */
  281.     gs_next_anim_page(dlist);            /* tell anim sys to use other bitmap */
  282.     gs_flip_display(display,1);        /* switch to other display, sync */
  283. }
  284.  
  285. /***************************************************************************/
  286.  
  287. void check_bounds()
  288.  
  289. {
  290.     int cnt;
  291.  
  292.     for (cnt=0; cnt < cplx_cnt; cnt++)
  293.         {
  294.         if (cplx[cnt].anim->flags & ANIM_BOUNDS_X1)
  295.             {
  296.             x[cnt]=cplx[cnt].anim->x;
  297.             speedx[cnt]*=-1;
  298.             gs_set_cplx_seq(&cplx[cnt],1,x[cnt],y[cnt]);    /* move right */
  299.             }
  300.         else if (cplx[cnt].anim->flags & ANIM_BOUNDS_X2)
  301.             {
  302.             x[cnt]=cplx[cnt].anim->x;
  303.             speedx[cnt]*=-1;
  304.             gs_set_cplx_seq(&cplx[cnt],0,x[cnt],y[cnt]);    /* move left */
  305.             }
  306.         if ((cplx[cnt].anim->flags & (ANIM_BOUNDS_Y1|ANIM_BOUNDS_Y2)) && (!(bounce[cnt])))
  307.             {
  308.             y[cnt]=cplx[cnt].anim->y;
  309.             speedy[cnt]*=-1;
  310.             bounce[cnt]=1;                    /* don't get "stuck" on top or bottom */
  311.             }
  312.         else
  313.             bounce[cnt]=0;
  314.         }
  315. }
  316.  
  317. /***************************************************************************/
  318.  
  319. int check_close()
  320.  
  321. /* check for user input */
  322.  
  323. {
  324.     if (gs_joystick(0) & JOY_BUTTON1)    /* IF (mouse button pressed) */
  325.         return(1);                            /* ..time to wrap up */
  326.     return(0);                                /* ELSE (keep going) */
  327. }
  328.  
  329. /***************************************************************************/
  330.  
  331. void cleanup()
  332.  
  333. /* release all resources and memory */
  334.  
  335. {
  336.     if (dlist > -1)
  337.         gs_free_display_list(dlist);
  338.     free_arrays();
  339.     gs_free_cplx(cplx,cplx_cnt);
  340.     gs_remove_display(display);
  341.     gs_close_vb_timer();
  342. }
  343.  
  344. /***************************************************************************/
  345.  
  346. void free_arrays()
  347.  
  348. {
  349.     if (bounce)
  350.         free(bounce);
  351.     if (speedy)
  352.         free(speedy);
  353.     if (speedx)
  354.         free(speedx);
  355.     if (y)
  356.         free(y);
  357.     if (x)
  358.         free(x);                            /* free up control arrays */
  359. }
  360.